#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

#endregion

namespace TVC.Forms
{
    public partial class frmMain : Form
    {
        string _fromType = "";
        string _emp = "";
        string _strAction = "";
        object _obj = null;

        public MainForm MainForm { get; set; }
        public object Obj
        {
            get { return _obj; }
            set { _obj = value; }
        }
        public string StrAction
        {
            get { return _strAction; }
            set { _strAction = value; }
        }
        public string EmpID
        {
            get { return _emp; }
            set { _emp = value; }
        }
        DateTime _minDate = new DateTime(2000, 1, 1);
        DateTime _maxDate = new DateTime(2100, 1, 1);
        private Control mobjCurrentHostedControl = null;
        private IHostedApplication mobjCurrentHostedApplication = null;
        NavigationTabs mobjTabsMains = null;

        public NavigationTabs TabsMains
        {
            get { return mobjTabsMains; }
            set { mobjTabsMains = value; }
        }
        public string FormType
        {
            get { return _fromType; }
            set { _fromType = value; }
        }
        public frmMain(MainForm form)
        {
            InitializeComponent();
            MainForm = form;

        }
        public frmMain(string type, MainForm form)
        {
            InitializeComponent();
            _fromType = type;
            MainForm = form;
        }
        public void SetTitle(string str)
        {
            this.Text = "";
            mobjPanelCategorie.Text = str;
        }

        public void SelectCategory(NavigationTabs mobjTabsMain, string moduleName)
        {
            if (mobjTabsMain == null)
            {
                BaseForm baseForm = MainForm as BaseForm;
                if (baseForm != null)
                    mobjTabsMain = baseForm.TabsMain;
            }
            if (mobjTabsMain.TabPages.Count > 0)
            {
                // Get the selected category tree-node from within the nav-tab 
                // treeview control and navigate to the selected control
                // if there is any.
                for (int i = 0; i < mobjTabsMain.TabPages.Count; i++)
                {
                    TreeView objTreeView = mobjTabsMain.TabPages[i].Controls[0] as TreeView;
                    if (objTreeView != null)
                    {
                        TreeNode[] arrNodes = objTreeView.Nodes.Find(moduleName, true);
                        if (arrNodes != null && arrNodes.Length > 0)
                        {
                            //objTreeView.SelectedNode = arrNodes[0];
                            this.SelectCategory(arrNodes[0].Tag as CategoryNode, true);
                        }

                    }
                }

            }

        }
        public void SelectCategory(CategoryNode objCategoryNode, bool blnBookmark)
        {

            if (objCategoryNode != null)
            {
                Control objControl = objCategoryNode.GetCategoryInstance();
                if (objControl != null)
                {
                    objControl.Dock = DockStyle.Fill;
                    AddWorspaceControl(objControl);

                    IHostedApplication objHostedApplication = objControl as IHostedApplication;

                    if (objHostedApplication != null)
                    {
                        CatalogSettings.InitializeCatalogModule(objHostedApplication, this.mobjToolBarMain, new EventHandler(objToolBarButton_Click));
                        if (StrAction == "C")
                        {
                            if (Obj == null || Obj.ToString() == "")
                                Obj = String.Format("MinDate={0:yyyy-MM-dd}&MaxDate={1:yyyy-MM-dd}", _minDate, _maxDate);
                            //objHostedApplication.addRecord();
                            objHostedApplication.SetObject(Obj);
                        }
                        else if (StrAction == "A")
                        {
                            Obj = Obj;//+ "&Action=A"
                            objHostedApplication.SetObject(Obj);
                        }
                        else if (StrAction == "O")
                        {
                            Obj = Obj;//+ "&Action=O"
                            objHostedApplication.SetObject(Obj);
                        }
                        SetTitle(objCategoryNode.Text);
                    }
                    else
                    {
                        if (this.mobjToolBarMain.Buttons.Count > 0)
                        {
                            this.mobjToolBarMain.Buttons.Clear();
                            this.mobjToolBarMain.Update();
                        }
                    }

                    mobjCurrentHostedControl = objControl;
                    mobjCurrentHostedApplication = objHostedApplication;
                }
                else
                {
                    this.mobjToolBarMain.Buttons.Clear();
                }
            }
            else Close();
        }
        private void objToolBarButton_Click(object sender, EventArgs e)
        {
            if (mobjCurrentHostedApplication != null)
            {
                CatalogSettings.HandleApplicationHostToolBarClick(mobjCurrentHostedApplication, this.mobjToolBarMain, (ToolBarButton)sender, e);
            }
        }
        private void AddWorspaceControl(Control objControl)
        {
            mobjPanelCategorie.Controls.Add(objControl);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            SelectCategory(mobjTabsMains, _fromType);
        }

        public void SetTimeRange(DateTime minDate, DateTime maxDate)
        {
            this._minDate = minDate;
            this._maxDate = maxDate;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (mobjPanelCategorie.Controls.Count > 0)
            {
                if (mobjPanelCategorie.Controls[0] is Modules.ModuleParent)
                {
                    Modules.ModuleParent aModules = mobjPanelCategorie.Controls[0] as Modules.ModuleParent;
                    aModules.Cancel = false;
                    if (aModules._processStatus == "C" || aModules._processStatus == "A" || aModules._processStatus == "O")
                    {
                        aModules.saveRecord(null);
                        if (aModules.Cancel == false)
                        {
                            this.DialogResult = Gizmox.WebGUI.Forms.DialogResult.OK;
                            Close();
                        }
                    }
                    else
                    {
                        this.DialogResult = Gizmox.WebGUI.Forms.DialogResult.OK;
                        Close();
                    }
                }
            }


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void lbErr_Click(object sender, EventArgs e)
        {
            if (lbErr.Text != "")
                MessageBox.Show(lbErr.Text);
        }



        public void ThrowMsg(string sErr)
        {
            lbErr.Text = sErr;
        }
    }
}